import 'dart:ui';
import 'package:factoryside/api/api.dart';
import 'package:factoryside/bean/add_order_entity.dart';
import 'package:factoryside/index.dart';
import 'package:factoryside/utils/ColorsUtils.dart';
import 'package:factoryside/utils/request_util.dart';
import 'package:factoryside/utils/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'bar_chart_sample.dart';

///确定发布

class ConfirmPage extends StatefulWidget {
  AddOrderEntity _addOrderEntity;
  bool isSend;
  String addr;
  String categoryName;
  String specificationsName;
  String brandName;
  String modelName;
  String partStr;
  String addrStr;

  ConfirmPage(this._addOrderEntity, this.isSend, this.addr, this.categoryName,
      this.specificationsName, this.brandName, this.modelName, this.partStr,
      this.addrStr);

  @override
  State<StatefulWidget> createState() {
    return _ConfirmState();
  }
}

class _ConfirmState extends State<ConfirmPage> with AutomaticKeepAliveClientMixin {

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context); //必须实现 不然 在push过后页面会刷新
    // TODO: implement build

    return Scaffold(
      appBar: AppBar(
        title: Text('确认发布'),
      ),
      body: Container(
        color: ColorsUtil.hexToColor('#EFEFEF'),
        child: Column(children: [
          Expanded(
            child: ListView(
              children: [
                Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius:
                    BorderRadius.all(Radius.circular(5))),
                  margin: EdgeInsets.all(10),
                  padding: EdgeInsets.all(10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        margin: EdgeInsets.all(5),
                        child: Row(
                          children: [
                            Text(widget._addOrderEntity.name),
                            SizedBox(width: 30,),
                            Expanded(child: Text(widget._addOrderEntity.phone,style: TextStyle(color: Colors.grey),)),
                          ],
                        ),
                      ),
                      Container(
                          margin: EdgeInsets.all(5),
                          child: Text(widget.addr,style: TextStyle(color: Colors.grey),)),
                    ],
                  ),
                ),
                Container(
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius:
                      BorderRadius.all(Radius.circular(5))),
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  padding: EdgeInsets.all(10),
                  child: Column(
                    children: [
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.all(5),
                        child: Row(
                          children: [
                            Text('服务类型：'),
                            SizedBox(
                              width: 10,
                            ),
                            Text(getServiceTypeStr(),style: TextStyle(color: Colors.orange),),
                          ],
                        ),
                      ),
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.all(5),
                        child: Row(
                          children: [
                            Text('售后类型：'),
                            SizedBox(
                              width: 10,
                            ),
                            Text(widget._addOrderEntity.guaranteetype==1?'保内':'保外',style: TextStyle(color: Colors.red),),
                          ],
                        ),
                      ),
                      Container(
                        decoration: BoxDecoration(
                            color: Color.fromARGB(180, 240, 240, 240),
                            borderRadius:
                            BorderRadius.all(Radius.circular(5))),
                        child: Row(
                          children: [
                            Expanded(
                              child: Column(
                                children: [
                                  Container(
                                    padding: EdgeInsets.all(5),
                                    child: Row(
                                      children: [
                                        Text('产品类别：'),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Text(widget.categoryName,style: TextStyle(color: Colors.grey),),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    padding: EdgeInsets.all(5),
                                    child: Row(
                                      children: [
                                        Text('产品规格：'),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Text(widget.specificationsName,style: TextStyle(color: Colors.grey),),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    padding: EdgeInsets.all(5),
                                    child: Row(
                                      children: [
                                        Text('产品品牌：'),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Text(widget.brandName,style: TextStyle(color: Colors.grey),),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    padding: EdgeInsets.all(5),
                                    child: Row(
                                      children: [
                                        Text('产品型号：'),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Text(widget.modelName,style: TextStyle(color: Colors.grey),),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(right: 10),
                                child: Text('x${widget._addOrderEntity.prods[0].num}'))
                          ],
                        ),
                      ),
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.all(5),
                        child: Row(
                          children: [
                            Text('条形码：'),
                            SizedBox(
                              width: 10,
                            ),
                            Text(widget._addOrderEntity.prods[0].factoryBarCode!=null?widget._addOrderEntity.prods[0].factoryBarCode:'暂无',style: TextStyle(color: Colors.grey),),
                          ],
                        ),
                      ),
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.all(5),
                        child: Row(
                          children: [
                            Text('购买时间：'),
                            SizedBox(
                              width: 10,
                            ),
                            Text(widget._addOrderEntity.prods[0].buyingProductsTime!=null?widget._addOrderEntity.prods[0].buyingProductsTime:'暂无',style: TextStyle(color: Colors.grey),),
                          ],
                        ),
                      ),
                      Visibility(
                        visible: widget._addOrderEntity.servicetype==2&&widget._addOrderEntity.guaranteetype==1,
                          child: Container(
                        color: Colors.white,
                        padding: EdgeInsets.all(5),
                        child: Row(
                          children: [
                            Text('快递单号：'),
                            SizedBox(
                              width: 10,
                            ),
                            Text(widget._addOrderEntity.prods[0].prodExpressNo!=null?widget._addOrderEntity.prods[0].prodExpressNo:'暂无',style: TextStyle(color: Colors.grey),),
                          ],
                        ),
                      )),
                      Visibility(
                        visible: widget._addOrderEntity.servicetype==1&&widget._addOrderEntity.guaranteetype==1,
                        child: Column(
                          children: [
                            Container(
                              color: Colors.white,
                              padding: EdgeInsets.all(5),
                              child: Row(
                                children: [
                                  Text('是否已发配件：'),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Text(widget.isSend?'是':'否',style: TextStyle(color: Colors.grey),),
                                ],
                              ),
                            ),
                            Visibility(
                              visible: widget.isSend,
                              child: Column(
                                children: [
                                  Container(
                                    color: Colors.white,
                                    padding: EdgeInsets.all(5),
                                    child: Row(
                                      children: [
                                        Text('已发配件：'),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Expanded(child: Text(widget.partStr==null?'暂无':widget.partStr,style: TextStyle(color: Colors.grey),)),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    color: Colors.white,
                                    padding: EdgeInsets.all(5),
                                    child: Row(
                                      children: [
                                        Text('配件快递单号：'),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Text(widget._addOrderEntity.prods[0].expressNo!=null?widget._addOrderEntity.prods[0].expressNo:'暂无',style: TextStyle(color: Colors.grey),),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    color: Colors.white,
                                    padding: EdgeInsets.all(5),
                                    child: Row(
                                      children: [
                                        Text('是否返件：'),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Text(widget._addOrderEntity.backParts!=null?(widget._addOrderEntity.backParts==1?'是':'否'):'',style: TextStyle(color: Colors.grey),),
                                      ],
                                    ),
                                  ),
                                  Visibility(
                                    visible: widget._addOrderEntity.backParts==1,
                                    child: Column(
                                      children: [
                                        Container(
                                          color: Colors.white,
                                          padding: EdgeInsets.all(5),
                                          child: Row(
                                            children: [
                                              Text('邮费支付方式：'),
                                              SizedBox(
                                                width: 10,
                                              ),
                                              Text(widget._addOrderEntity.postpaytype!=null?(widget._addOrderEntity.postpaytype==1?'工厂到付':'师傅现付'):'',style: TextStyle(color: Colors.grey),),
                                            ],
                                          ),
                                        ),
                                        Container(
                                          color: Colors.white,
                                          padding: EdgeInsets.all(5),
                                          child: Row(
                                            children: [
                                              Text('旧件收件地址：'),
                                              SizedBox(
                                                width: 10,
                                              ),
                                              Expanded(child: Text(widget.addrStr==null?'暂无':widget.addrStr,style: TextStyle(color: Colors.grey),)),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius:
                      BorderRadius.all(Radius.circular(5))),
                  margin: EdgeInsets.all(10),
                  padding: EdgeInsets.all(10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                          padding:
                          EdgeInsets.all(5),
                          child: Text('服务要求：')),
                      SizedBox(
                        width: 10,
                      ),
                      Container(
                          padding:
                              EdgeInsets.all(10),
                          width: double.infinity,
                          decoration: BoxDecoration(
                              color: Color.fromARGB(180, 240, 240, 240),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(3))),
                          child: Text(widget._addOrderEntity.prods[0].bak
//                              '2020-11-16 不制冷 请师傅在保修卡上填写维修记录并和机器条形码拍照上传，要求照片清晰，没照片不结算，如遇小冰箱（BCD开头）压缩机故障不修，开鉴定处理-01'
                          )),
                    ],
                  ),
                ),
              ],
            ),
          ),
          Container(
              color: Colors.white,
              padding: EdgeInsets.fromLTRB(0, 5, 20, 5),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  ElevatedButton(
                      onPressed: () {
                        RequestUtil.post(Api.addCheck, {'Phone':widget._addOrderEntity.phone}).then((value){
                          if(value['Data']['code']==0){
                            addOrder();
                          }else{
                            showDialog<Null>(
                              context: context,
                              barrierDismissible: true,
                              builder: (context) {
                                return AlertDialog(
                                  title: Center(
                                    child: Text(value['Data']['msg']),
                                  ),
                                  titleTextStyle: TextStyle(fontSize: 16,color: Colors.black),
                                  actions: <Widget>[
                                    TextButton(
                                      child: Text('要'),
                                      onPressed: () {
                                        Navigator.of(context).pop();
                                        addOrder();
                                      },
                                    ),
                                    TextButton(
                                      child: Text('再想想'),
                                      onPressed: () {
                                        Navigator.of(context).pop();
                                      },
                                    ),
                                  ],
                                );
                              },
                            );
                          }
                        });
//                        Navigator.push(this.context,
//                            CupertinoPageRoute(builder: (BuildContext context) {
////                                  return ShowCase();
//                          return ConfirmPage();
//                        }));
                      },
                      child: Text('确认发布')),
                ],
              )),
        ]),
      ),
    );
  }

  void addOrder() {
    RequestUtil.post(Api.addOrder, widget._addOrderEntity.toJson()).then((value){
      if(value['Data']['code']==0){
        showToast('发布成功');
        ///跳转到起始页并关闭所有页面
        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(builder: (context) => Index(currentIndex: 1)),
              (Route<dynamic> route) => false,
        );
      }else{
        showToast(value['Data']['msg']);
      }
    });
  }

  String getServiceTypeStr() {
   if(widget._addOrderEntity.servicetype==1){
     return '上门维修';
   }else if(widget._addOrderEntity.servicetype==2){
     return '上门安装';
   }else{
     return '用户送修';
   }
  }
}
